<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/9/29
 * Time: 15:37
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Order extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [];
            if(isset($user_name) && $user_name != ''){
                $where['info_designer|info_info'] = ['like', '%' . $user_name . '%'];
            }

            if(isset($info_case_id) && $info_case_id != ''){
                $where['info_case_id'] = $info_case_id;
            }
            if(isset($info_t_id) && $info_t_id != ''){
                $where['info_t_id'] = $info_t_id;
            }
            if(isset($info_pp_id) && $info_pp_id != ''){
                $where['info_pp_id'] = $info_pp_id;
            }
            if(isset($info_p_id) && $info_p_id != ''){
                $where['info_p_id'] = $info_p_id;
            }
            if(isset($info_s_id) && $info_s_id != ''){
                $where['info_s_id'] = $info_s_id;
            }
            if(isset($info_user_id) && $info_user_id != ''){
                $where['info_user_id'] = $info_user_id;
            }
            if(isset($laiyuan) && $laiyuan != ''){
                if($laiyuan == 1){
                    $where['info_s_id'] = ['>',0];
                }else{
                    $where['info_s_id'] = 0;
                }

            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['info_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['info_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['info_time'] = ['between time',[$start,$end]];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_case_info info')
                ->join('up_case c', 'info.info_case_id = c.case_id', 'left')
                ->join('up_types t', 'info.info_t_id = t.t_id', 'left')
                ->join('up_pp pp', 'info.info_pp_id = pp.pp_id', 'left')
                ->join('up_store s', 'info.info_s_id = s.s_id', 'left')
                ->join('up_place p', 'info.info_p_id = p.p_id', 'left')
                ->join('up_u_user u', 'info.info_user_id = u.user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $field = 'info.*,case_name,t_name,pp_name,s_name,p_name,user_d_name';
            $lists = Db::table('up_case_info info')
                ->join('up_case c', 'info.info_case_id = c.case_id', 'left')
                ->join('up_types t', 'info.info_t_id = t.t_id', 'left')
                ->join('up_pp pp', 'info.info_pp_id = pp.pp_id', 'left')
                ->join('up_store s', 'info.info_s_id = s.s_id', 'left')
                ->join('up_place p', 'info.info_p_id = p.p_id', 'left')
                ->join('up_u_user u', 'info.info_user_id = u.user_id', 'left')
                ->field($field)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('info_sort desc,info_time desc')
                ->select();
            $allmoney = 0;
            if($lists){
                foreach ($lists as $k=>$v){
                    $allmoney += $v['info_money'];
                    if($v['info_img']){
                        $lists[$k]['info_img'] = explode(',',$v['info_img']);
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists, 'allmoney' => $allmoney, 'allnums' => $count]);
        }
        $types = Db::table('up_types')
            ->field('t_id,t_name')
            ->order('t_sort desc')
            ->select();
        $pps = Db::table('up_pp')
            ->field('pp_id,pp_name')
            ->order('pp_sort desc')
            ->select();
        $ps = Db::table('up_place')
            ->field('p_id,p_name')
            ->order('p_sort desc')
            ->select();
        $sss = Db::table('up_store')
            ->field('s_id,s_name')
            ->order('s_sort desc')
            ->select();
        $cases = Db::table('up_case')
            ->field('case_id,case_name')
            ->order('case_sort desc')
            ->select();
        $this->assign('types',$types);
        $this->assign('pps',$pps);
        $this->assign('ps',$ps);
        $this->assign('sss',$sss);
        $this->assign('cases',$cases);
        $users2 = Db::table('up_u_user')
            ->where('user_status',1)
            ->where('user_d_type',2)
            ->field('user_id,user_d_name,user_phone')
            ->select();
        $this->assign('users2',$users2);
        return $this->fetch();
    }

    /**
     * 订单详情
     */
    public function orderInfo(){
        if(request()->isAjax()){
            $info_s_id = input('info_s_id');
            $type = input('type');

            if($type == 1){
                $where = [
                    'info_s_id' => $info_s_id
                ];
            }else{
                $where = [
                    'info_user_id' => $info_s_id
                ];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_case_info info')
                ->join('up_case c', 'info.info_case_id = c.case_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $field = 'info.*,case_name';
            $lists = Db::table('up_case_info info')
                ->join('up_case c', 'info.info_case_id = c.case_id', 'left')
                ->field($field)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('info_sort desc,info_time desc')
                ->select();
            $allmoney = 0;
            if($lists){
                foreach ($lists as $k=>$v){
                    $allmoney += $v['info_money'];
                    if($v['info_img']){
                        $lists[$k]['info_img'] = explode(',',$v['info_img']);
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists, 'allmoney' => $allmoney, 'allnums' => $count]);
        }
        $info_s_id = input('info_s_id');
        $type = input('type');
        $this->assign('info_s_id',$info_s_id);
        $this->assign('type',$type);
        return $this->fetch('order/case');
    }
    /**
     * 推荐
     * @return \think\response\Json
     */
    public function changeMainStatus(){
        $id = input('id');
        $param = [
            'info_top'=>input('num'),
        ];
        $res = Db::table('up_case_info')->where('info_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 删除订单
     * @return \think\response\Json
     */
    public function orderDel(){
        $order_id = input('id');
        $can = Db::table('up_self_order')
            ->where('order_id',$order_id)
            ->field('is_pay,order_cancel_pay')
            ->find();
        if($can['is_pay'] == 2 && $can['order_cancel_pay'] == 2){
            return json(['code' => 100, 'msg' => '此订单还未退款，不可删除']);
        }
        $res = Db::table('up_self_order')
            ->where('order_id',$order_id)
            ->setField('is_delete',1);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }



    /**
     * 查看评价
     * @return mixed|\think\response\Json
     */
    public function orderComments(){
        if(request()->isAjax()){
            extract(input());
            $order_id = input('order_id');
            $contract = input('contract');
            $good_table = 'up_self_goods';
            $goods_comment_table = 'up_self_goods_comment';
            if($contract != 1){
                $good_table = 'up_goods';
                $goods_comment_table = 'up_goods_comment';
            }
            //
            $where = [
                'comment_order_id' => $order_id
            ];
            $count = Db::table($goods_comment_table)->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.comment_user_id','left')
                ->join("$good_table g",'a.comment_good_id=g.good_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table($goods_comment_table)->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.comment_user_id','left')
                ->join("$good_table g",'a.comment_good_id=g.good_id','LEFT')
                ->where($where)
                ->field('a.*,uuu.user_username,uuu.user_phone,good_name')
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    if($v['comment_image']){
                        $lists[$k]['comment_image'] = explode(',',$v['comment_image']);
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $order_id = input('order_id');
        $contract = Db::table('up_self_order')->where('order_id', $order_id)->value('order_contract');
        $this->assign('order_id',$order_id);
        $this->assign('contract',$contract);
        return $this->fetch('order/ordercomments');
    }
    /**
     * 查看物流列表
     * @return mixed|\think\response\Json
     */
    public function orderWuliu(){
        if(request()->isAjax()){
            extract(input());
            $order_id = input('order_id');
            //
            $contract = Db::table('up_self_order')->where('order_id', $order_id)->value('order_contract');
            $good_table = 'up_self_goods';
            if($contract != 1){
                $good_table = 'up_goods';
            }
            $count = Db::table('up_self_order_detail')->alias('od')
                ->join("$good_table g",'od.goods_id=g.good_id','LEFT')
                ->where('order_id',$order_id)
                ->count();// 获取总条数
            $field = 'num,unit_price,goods_info,good_id,good_name,detail_id,detail_express,detail_express_no';
            $lists = Db::table('up_self_order_detail')->alias('od')
                ->join("$good_table g",'od.goods_id=g.good_id','LEFT')
                ->where('order_id',$order_id)
                ->field($field)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $order_id = input('order_id');
        $contract = Db::table('up_self_order')->where('order_id', $order_id)->value('order_contract');
        $this->assign('order_id',$order_id);
        $this->assign('contract',$contract);
        return $this->fetch('order/wuliu');
    }
    /**
     * 查看物流详情
     * @return mixed|\think\response\Json
     */
    public function wuliuInfo(){
        $code = input('express_no');
        $info = orderWuLiu($code);
//        dump($info);
        $this->assign('info',$info);
        return $this->fetch('order/wuliuinfo');
    }
    /**
     * 查看发票
     * @return mixed|\think\response\Json
     */
    public function fapiao(){
        $order_id = input('order_id');
        $info = Db::table('up_self_order')->where('order_id', $order_id)->find();
        $this->assign('info',$info);
        return $this->fetch('order/fapiao');
    }

    //导出
    public function excelOutOrder()
    {
        set_time_limit(0);//防止超时
        //ini_set("memory_limit", "512M");//防止内存溢出
        extract(input());
        $map = [];
        $map['is_delete'] = 0;
        $map['order_contract'] = ['<>',3];
        if(isset($n_title)&&$n_title!="")
        {
            $map['order_no|name|mobile|express_no'] = ['like',"%" . $n_title . "%"];
        }
        if(isset($contract)&&$contract!="")
        {
            $map['order_contract'] = $contract;
        }
        if(isset($orderid)&&$orderid!="")
        {
            $map['order_id'] = $orderid;
        }
        if(isset($type) && $type){
            if($type == 1){
                $map['is_pay'] = 0;
            }else{
                $map['is_pay'] = 1;
                if($type == 2){
                    $map['is_send'] = 0;
                }
                if($type == 3){
                    $map['is_send'] = 1;
                }
                if($type == 4){
                    $map['is_send'] = 2;
                }
                if($type == 5){
                    $map['is_send'] = 3;
                }
            }
        }
        if(isset($oldnew)&&$oldnew!="")
        {
            $map['new_old'] = $oldnew;
        }
        if(isset($schoolid) && $schoolid != ''){
            $map['s.school_erp'] = $schoolid;
        }
        if(isset($start)&&$start!=""&&isset($end)&&$end=="")
        {
            $map['pay_time'] = ['>= time',$start];
        }
        if(isset($end)&&$end!=""&&isset($start)&&$start=="")
        {
            $map['pay_time'] = ['<= time',$end];
        }
        if(isset($start)&&$start!=""&&isset($end)&&$end!="")
        {
            $map['pay_time'] = ['between time',[$start,$end]];
        }
        $field = [
            'order_no','order_back_order_id','total_price','express_original_price','all_goods_price',
            'all_good_nums','name','mobile','address','remark',
            'created_at','is_pay','pay_type','pay_time','school_name',
            'is_send','send_time','express','express_no','order_contract',
            'order_cancel_status','order_cancel_pay','order_cancel_money','order_erp_year','order_erp_done',
            'new_old'
        ];
        $data = Db::table('up_self_order')->alias('o')
            ->join('up_u_user u','o.user_id=u.user_id','LEFT')
            ->join('up_school s','o.order_school=s.school_erp','LEFT')
            ->where($map)
            ->field($field)
            ->select();
        if($data){
            $cellname = [
                '订单号', '第三方支付平台订单号', '总价（含邮费）', '运费', '衣服总价',
                '衣服总数量', '收件人', '收件电话', '收件地址', '备注',
                '创建订单时间', '是否支付', '支付平台', '支付时间', '学校名字',
                '是否发货', '发货时间', '发货物流', '物流单号', '订单类型',
                '订单是否取消', '是否已退款（取消了订单的才有）', '退款金额（取消了订单的才有）', '年份', '交期',
                '新老订单'
            ];
            $status = ['is_pay','pay_type','is_send','order_contract','order_cancel_status','order_cancel_pay','new_old'];
            $values = [
                 'is_pay' => [0 => '待支付',1 => '已支付',2 => '已取消']
                ,'pay_type' => [1 => '微信',2 => '支付宝',3 => '银联']
                ,'is_send' => [0 => '未发货',1 => '已发货待收货',2 => '已收货待评价',3 => '已完成']
                ,'order_contract' => [1 => '自营订单',2 => '增补订单']
                ,'order_cancel_status' => [1 => '未取消',2 => '取消被拒',3 => '已同意取消',4 => '待审核']
                ,'order_cancel_pay' => [1 => '正常',2 => '未打款',3 => '已打款']
                ,'new_old' => [1 => '新APP订单',2 => '旧APP订单']
            ];
            $res = exportExcel('order',time(),$cellname,$data,$status,$values);
            return json($res);
        }else{
            return json(['code'=>0,'msg'=>'此条件下无数据']);
        }
    }
    //导出
    public function excelOutDetail()
    {
        set_time_limit(0);//防止超时
        //ini_set("memory_limit", "512M");//防止内存溢出
        extract(input());
        $map = [];
        $map['order_contract'] = ['<>',3];
        if(isset($n_title)&&$n_title!="")
        {
            $map['order_no|name|mobile|express_no'] = ['like',"%" . $n_title . "%"];
        }
        if(isset($contract)&&$contract!="")
        {
            $map['order_contract'] = $contract;
        }
        if(isset($orderid)&&$orderid!="")
        {
            $map['order_id'] = $orderid;
        }
        if(isset($type) && $type){
            if($type == 1){
                $map['is_pay'] = 0;
            }else{
                $map['is_pay'] = 1;
                if($type == 2){
                    $map['is_send'] = 0;
                }
                if($type == 3){
                    $map['is_send'] = 1;
                }
                if($type == 4){
                    $map['is_send'] = 2;
                }
                if($type == 5){
                    $map['is_send'] = 3;
                }
            }
        }
        if(isset($oldnew)&&$oldnew!="")
        {
            $map['new_old'] = $oldnew;
        }
        if(isset($schoolid) && $schoolid != ''){
            $map['s.school_erp'] = $schoolid;
        }
        if(isset($start)&&$start!=""&&isset($end)&&$end=="")
        {
            $map['pay_time'] = ['>= time',$start];
        }
        if(isset($end)&&$end!=""&&isset($start)&&$start=="")
        {
            $map['pay_time'] = ['<= time',$end];
        }
        if(isset($start)&&$start!=""&&isset($end)&&$end!="")
        {
            $map['pay_time'] = ['between time',[$start,$end]];
        }
        $field = [
            'o.order_no','unit_price','num','attr_id','goods_info',
            'goods_coding_unique','name','mobile','address','remark',
            'o.created_at','is_pay','pay_type','pay_time','school_name',
            'is_send','send_time','express','express_no','order_contract',
            'order_cancel_status','order_cancel_pay','order_cancel_money','od.pay_year','order_erp_done',
            'o.new_old','od.th_status'
        ];
        $data = Db::table('up_self_order')->alias('o')
            ->join('up_self_order_detail od','o.order_id=od.order_id','LEFT')
            ->join('up_u_user u','o.user_id=u.user_id','LEFT')
            ->join('up_school s','o.order_school=s.school_erp','LEFT')
            ->where($map)
            ->field($field)
            ->select();
        if($data){
            $cellname = [
                '订单号', '单价', '数量', '尺码', '产品信息',
                '唯一条码', '收件人', '收件电话', '收件地址', '备注',
                '创建订单时间', '是否支付', '支付平台', '支付时间', '学校名字',
                '是否发货', '发货时间', '发货物流', '物流单号', '订单类型',
                '订单是否取消', '是否已退款（取消了订单的才有）', '退款金额（取消了订单的才有）', '年份', '交期',
                '新老订单','是否售后（只针对此条明细）'
            ];
            $status = ['is_pay','pay_type','is_send','order_contract','order_cancel_status','order_cancel_pay','new_old','th_status'];
            $values = [
                 'is_pay' => [0 => '待支付',1 => '已支付',2 => '已取消']
                ,'pay_type' => [1 => '微信',2 => '支付宝',3 => '银联']
                ,'is_send' => [0 => '未发货',1 => '已发货待收货',2 => '已收货待评价',3 => '已完成']
                ,'order_contract' => [1 => '自营订单',2 => '增补订单']
                ,'order_cancel_status' => [1 => '未取消',2 => '取消被拒',3 => '已同意取消',4 => '待审核']
                ,'order_cancel_pay' => [1 => '正常',2 => '未打款',3 => '已打款']
                ,'new_old' => [1 => '新APP订单',2 => '旧APP订单']
                ,'th_status' => [0 => '无售后',1 => '换货',2 => '退货']
            ];
            $res = exportExcel('orderinfo',time(),$cellname,$data,$status,$values);
            return json($res);
        }else{
            return json(['code'=>0,'msg'=>'此条件下无数据']);
        }
    }
}